package edu.whu.puppygo.service.Impl;

import edu.whu.puppygo.config.OssConfig;
import edu.whu.puppygo.dao.PostDao;
import edu.whu.puppygo.dto.PostDto;
import edu.whu.puppygo.entity.PetMatter;
import edu.whu.puppygo.entity.Post;
import edu.whu.puppygo.entity.User;
import edu.whu.puppygo.exception.BackendException;
import edu.whu.puppygo.service.PostService;
import edu.whu.puppygo.util.FileUtil;
import edu.whu.puppygo.util.GlobalUser;
import edu.whu.puppygo.util.OssUtil;
import edu.whu.puppygo.util.VoConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Resource
    private PostDao postDao;

    @Resource
    private OssUtil ossUtil;

    /**
     * 分页查询所有帖子
     * @param pageable
     * @return
     */
    public List<Post> getAllPosts(Pageable pageable){
        List<Post> posts = postDao.findAllByDeleteTimeNull(pageable);
        return posts;
    }

    /**
     * 根据用户昵称查询帖子
     * @param nickname
     * @param pageable
     * @return
     */
    @Override
    public List<Post> getAllPostsByNickname(String nickname, Pageable pageable) {
        List<Post> posts = postDao.findByNickname(nickname, pageable);
        return posts;
    }

    /**
     * 根据用户id查询帖子
     * @param userId
     * @param pageable
     * @return
     */
    @Override
    public List<Post> getAllPostsByUserId(Long userId, Pageable pageable) {
        List<Post> posts = postDao.findByUserId(userId, pageable);
        return posts;
    }

    /**
     * 发布帖子
     * @param postDto
     * @return
     */
    public Post publishPost(PostDto postDto, MultipartFile mpfile){

        User user = GlobalUser.getUser();
        Post post = VoConvertUtil.convertEntity(postDto, Post.class);

        if (mpfile != null) {
            String originalFileName = mpfile.getOriginalFilename();
            originalFileName = FileUtil.RandomFileName(originalFileName);
            String fileName = OssConfig.getPetPicPath() + originalFileName;
            String uploadedFileName = ossUtil.uploadFile(mpfile, fileName);
            post.setImageUrl(OssConfig.getFullUrl() + uploadedFileName);
        }

        post.setUserId(user.getId());
        post.setNickname(user.getNickname());
        post.setAvatar(user.getAvatar());
        Post publishedPost = postDao.save(post);
        return publishedPost;
    }

    /**
     * 更新帖子
     * @param post
     * @return
     */
    @Override
    public Post updatePost(Post post) {
        Long userId = GlobalUser.getUser().getId();
        post.setUserId(userId);
        Post res = postDao.save(post);
        return res;
    }

    /**
     * 删除帖子
     * @param postId
     */
    @Override
    public void deletePost(Long postId) {
        Post post = postDao.findById(postId).orElseThrow(() -> new BackendException("Post not found"));
        post.setDeleteTime(LocalDateTime.now());
        postDao.save(post);
    }


}
